package com.bnz.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.bnz.pojo.PageResult;
import com.bnz.pojo.R;
import com.bnz.pojo.Student;
import com.bnz.service.StudentService;
import com.bnz.service.impl.StudentServiceImpl;

import com.bnz.vo.StudentVo;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;
	// private int pageSize;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	//1. 单个servlet的多个请求方法的转发
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1.1 设置请求参数的编码
		req.setCharacterEncoding("UTF-8");      // 解决post请求的中文乱码问题
		//1.2 设置响应参数的字符编码类型
		resp.setContentType("text/html;charset=utf8");
		//1.3 得到请求参数
		String cmd = req.getParameter("cmd");
		//1.4 根据参数调用不同的方法
		if(StringUtils.isNotBlank(cmd)){
			if("list".equals(cmd)){             // 查看所有
				list(req,resp);
			}
			if("findByPage".equals(cmd)){      // 分页
				findByPage(req,resp);
			}
			if("search".equals(cmd)){          // 条件查询带分页
				search(req,resp);
			}
			if("add".equals(cmd)){              // 添加学生
				add(req,resp);
			}
		}
	}


	//2. 返回用户列表
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//2.1 得到所有的学生
		List<Student> students = studentService.findAll();
		//2.2 转换为json字符串
		String s = JSON.toJSONString(students);
		//2.3 将字符串输出到网页中
		resp.getWriter().println(s);
		//2.4 刷新流
		resp.getWriter().flush();
	}

	//3. 普通分页
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//3.1 得到分页参数
		//3.1.1 得到当前页
		String pageStr = req.getParameter("page");
		int page = StringUtils.isNotBlank(pageStr) ? Integer.parseInt(pageStr) : 1;     // 当前页
		//3.1.2 得到每页大小
		String pagesizeStr = req.getParameter("pagesize");                        // 每页大小
		int pagesize = StringUtils.isNotBlank(pagesizeStr) ? new Integer(pagesizeStr) : 3;
		//3.2 开始分页(方法一：返回的是PageInfo对象)
		// PageInfo<Student> pageInfo = studentService.findByPage2(page,pagesize);
		PageResult<Student> pageResult = studentService.findByPage(page, pagesize);
		//3.3 将pageInfo转换为json字符串
		String s = JSON.toJSONString(pageResult);
		//3.4 将字符串输出到网页中
		resp.getWriter().println(s);
		//3.5 刷新流
		resp.getWriter().flush();

	}

	//4. 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//4.1 得到分页参数
		//4.1.1 得到当前页
		String pageStr = req.getParameter("page");
		int page = StringUtils.isNotBlank(pageStr) ? Integer.parseInt(pageStr) : 1;     // 当前页
		//4.1.2 得到每页大小
		String pagesizeStr = req.getParameter("pagesize");                        // 每页大小
		int pagesize = StringUtils.isNotBlank(pagesizeStr) ? new Integer(pagesizeStr) : 3;
		//4.2 得到查询关键字
		String studSearch = req.getParameter("studSearch");
		//4.3 处理中文乱码
		studSearch = new String(studSearch.getBytes("ISO8859-1"),"UTF-8");
		System.out.println("studSearch = " + studSearch);
		//4.3 将查询学生的字符串转换为学生对象
		StudentVo studentVo = JSON.parseObject(studSearch, StudentVo.class);
		//4.4 开始查询得到分页结果
		PageResult<Student> pr = studentService.search(page,pagesize,studentVo);
		//4.5 将pageInfo转换为json字符串
		String s = JSON.toJSONString(pr);
		//4.6 将字符串输出到网页中
		resp.getWriter().println(s);
		//4.7 刷新流
		resp.getWriter().flush();

	}

	//5. 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String s = "";
		try {
			//5.1 得到添加的学生信息
			String stud = req.getParameter("stud");
			stud = new String(stud.getBytes("ISO8859-1"),"UTF-8");
			//5.2 转换为学生对象
			Student student = JSON.parseObject(stud,Student.class);
			//5.3 添加学生
			studentService.add(student);
			//5.4 后端返回一个R对象
			R ok = R.ok();
			s = JSON.toJSONString(ok);
		} catch (IOException e) {
			e.printStackTrace();
			R error = R.error();
			s = JSON.toJSONString(error);
		}
		resp.getWriter().println(s);
		resp.getWriter().flush();
	}

}
